package se.webbzon.boltzmann;

import se.webbzon.boltzmann.configuration.Controls;
import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.font.FontLoaderMap;
import se.webbzon.boltzmann.game.character.AbstractCharacter;
import se.webbzon.boltzmann.game.character.CharacterLoader;
import se.webbzon.boltzmann.imageset.ImageSetLoaderMap;
import se.webbzon.boltzmann.input.GamepadInstaller;
import se.webbzon.boltzmann.input.Input;
import se.webbzon.boltzmann.input.KeyboardInput;
import se.webbzon.boltzmann.loader.LoaderException;
import se.webbzon.boltzmann.modelset.ModelSetLoaderMap;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.boltzmann.sound.SoundLoaderMap;
import se.webbzon.boltzmann.textureset.TextureSetLoaderMap;
import se.webbzon.oschi01.worldarea.WorldEngine;
import se.webbzon.oschi01.worldarea.DefaultWorldEngineTimer;
import se.webbzon.oschi01.worldarea.WorldLiveness;
import se.webbzon.oschi01.worldcamera.SimpleWorldRenderer;
import se.webbzon.oschi01.worldcamera.WorldRenderer;
import se.webbzon.oschi01.worldcamera.WorldViewFactory;

public class Main {
	
	// The current game window
	private static GameWindow gameWindow;
	
	public static void main(String[] args) {
		
		/* Setup the world engine. */
		WorldEngine engine = new WorldEngine();
		WorldLiveness.instance().register(engine);
		//DefaultWorldEngineTimer timer = new DefaultWorldEngineTimer(25);
		//timer.attachEngine(engine);
		//WorldViewFactory factory = new WorldViewFactory(WorldViewFactory.TRANSPARENCY,engine,timer);
		SimpleWorldRenderer renderer = SimpleWorldRenderer.instance();
		renderer.attachEngine(engine);
		
		/* Create loader window. */
		LoadWindow win = new LoadWindow(renderer);
		
		/* Load the resources to the game. */
		ResourceLoader loader = new ResourceLoader();
		
		ImageSetLoaderMap islm = new ImageSetLoaderMap();
		TextureSetLoaderMap tslm = new TextureSetLoaderMap();
		CharacterLoader cl = new CharacterLoader(AbstractCharacter.getCharacterTextureSetMap());
		ModelSetLoaderMap mslm = new ModelSetLoaderMap();
		SoundLoaderMap slm = new SoundLoaderMap();
		FontLoaderMap flm = new FontLoaderMap();
		try {
			loader.load("gfx/resources.xml", islm, tslm, cl);
			loader.load("models/resources.xml", mslm);
			loader.load("sfx/resources.xml", slm);
			loader.load("fonts/resources.xml", flm);
		} catch (LoaderException e) {
			Debug.report(e);
		}
		Resources.setAlerting(true);
		Resources.setImageSetMap(islm.getMap());
		Resources.setTextureSetMap(tslm.getMap());
		Resources.setModelSetMap(mslm.getMap());
		Resources.setSoundMap(slm.getMap());
		Resources.setFontMap(flm.getMap());
		
		/* Add all the available input to the control configuration. */
		Controls controls = Controls.instance();
		KeyboardInput keyboard = new KeyboardInput();
		controls.addInput(keyboard);
		controls.setInput(keyboard);
		
		if (args.length == 0 || !args[args.length-1].equals("-nojinput")) {
			GamepadInstaller installer = new GamepadInstaller();
			installer.installInputs();
			for (Input i : installer.getInputs()) {
				controls.addInput(i);
				controls.setInput(i);
			}
		}
		
		/* Start the game. */
		renderer.closeWorldView();
		win.dispose();
		
		gameWindow = new GameWindow(renderer,keyboard);
	}
	
	/** Returns the default game window. **/
	public static final GameWindow getGameWindow() {
		return gameWindow;
	}

}
